package cn.zysheep.observer.java;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: Observerable
 * @Author: lyw
 * @Date: 2022/10/27 14:34
 * @Description: TODO(一句话描述该类的功能)
 */
public class Observerable {
    private List<Observer> observers = new ArrayList<Observer>();

    private int state;

    public int getState() {
        return state;
    }

    public void setState(int state) {
        notifyAllObservers(state);
    }

    //添加观察者
    public void addServer(Observer observer){
        observers.add(observer);
    }

    //移除观察者
    public void removeServer(Observer observer){
        observers.remove(observer);
    }
    //通知
    public void notifyAllObservers(int state){
        if(state != 1){
            System.out.println("不是通知的状态");
            return ;
        }

        for (Observer observer : observers) {
            observer.doEvent();
        }
    }
}
